﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Acando.Dynamics.CRM.Silverlight.Library.Helper;

namespace Acando.Dynamics.CRM.Silverlight.Library.Dialogs
{
    public partial class IncreaseStorageDialog : UserControl
    {
        private Panel Parent { get; set; }
        private long TargetSpace {get; set;}

        public IncreaseStorageDialog(long targetSpace)
        {
            InitializeComponent();

            this.Parent = ApplicationContext.Current.GetParentForDialogs();
            this.TargetSpace = targetSpace;
            this.btnIncrease.Click += (sender, args) => this.increaseIsoStorage();
            this.btnCancel.Click += (sender, args) => this.CloseControl(); 
        }

        private void increaseIsoStorage()
        {
          
            if (IsolatedStoreHelper.CurrentSpaceIsLessThan(this.TargetSpace))
            {
                var responseFromUser = IsolatedStoreHelper.AskUserToIncreaseQuota(this.TargetSpace);

                if (!responseFromUser)
                    this.txtB_messages.Text = "Not a good idea.";
                else
                    this.CloseControl();
            }
            else
                this.txtB_messages.Text = "Maximum is reached, increase is not nessary.";
        }

        public void ShowDialog()
        {
            if (!this.Parent.Children.Contains(this))
            {
                this.Parent.Children.Add(this);

                this.Visibility = Visibility.Visible;

                this.IsEnabled = true;
            }
        }

        private void CloseControl()
        {
            this.Parent.Children.Remove(this);
            this.Visibility = Visibility.Collapsed;
        }
    }
}
